/* Author: Mark Mckillion, Chris Mowbray
 * Date: 17/04/2012
 * Version 1.0.*/

/*
 * This is the Inserion Sorting Algorithm this is used by considering the elements one 
 * at a time, inserting each in its suitable place among those already considered .  
 * it builds the sorted sequence one numberat a time.where we build up a complicated 
 * structure on n items by first building it on n - 1 items and then making the 
 * necessary changes to fix things in adding the last item. 
 */

package assignment4;

import java.awt.Color;
import java.awt.Image;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class InsertionSort {
    
     // as mentioned other classes we get the current text from the textpane and
    // create a styled document so be edited
    static StyledDocument sdoc = ModelFrame.algoritm.getStyledDocument();
    static SimpleAttributeSet attrs = new SimpleAttributeSet();
    // int that i sused to sleep the current thread, updated by the slider
    public static int speed;
    // get the paragraph
    static Element section = ModelFrame.algoritm.getDocument().getDefaultRootElement();
    // get the number of lines
    int paraCount = section.getElementCount();
    
    // the Insertion sort algorithm with 'Thread.sleep' throughout so that this sorting
    // algoritm will be slowed down.
    // for the colurs
    public static void sortObjects(Color[] list) {
        list = Colour.colors;
        int position;
        DrawingPanel.comp1 = 0;
        DrawingPanel.comp2 = 0;
        Color temp;
        
        highlight(3);
        try {
            Thread.sleep(speed);
        } catch (Exception e) {
        }
        deHighlight(3);
        for (int index = 0; index < list.length; index++) {
                                   
            ModelFrame.drawing.repaint();
            
            highlight(5);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(5);
            
            temp = list[index];
            
            highlight(6);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(6);
            
            position = index;
                        
            highlight(7);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(7);
            
            DrawingPanel.comp1 = index;
            ModelFrame.drawing.repaint();
            
            while (position > 0 && Colour.colour.compareTo(list[position - 1], temp) == 1) 
            {
                highlight(9);
                try {
                    Thread.sleep(speed);
                } catch (Exception e) {
                }
                deHighlight(9);
                
                list[position] = list[position - 1];
                ModelFrame.drawing.repaint();
                highlight(10);
                try {
                    Thread.sleep(speed);
                } catch (Exception e) {
                }
                deHighlight(10);
                
                position--;
                DrawingPanel.comp2 = position;
                ModelFrame.drawing.repaint();
            }
            
            list[position] = temp;
            ModelFrame.drawing.repaint();
            
            highlight(12);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(12);
                        
            ModelFrame.drawing.repaint();
        }
        DrawingPanel.sn = list.length;
        ModelFrame.drawing.repaint();
        highlight(14);
    }
    
    // the same sorting algoritm but using the vehicles
    public static void sortObjects(Image [] list) {
        list = Vehicle.vehicles;
        int position;
        DrawingPanel.comp1 = 0;
        DrawingPanel.comp2 = 0;
        Image temp;
        // we highlight line three
        highlight(3);
        // sleep the thread for a small period of time
        try {
            Thread.sleep(speed);
        } catch (Exception e) {
        }
        // then dehighlight the line
        deHighlight(3);
        for (int index = 0; index < list.length; index++) {
                                   
            ModelFrame.drawing.repaint();
            
            highlight(5);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(5);
            
            temp = list[index];
            
            highlight(6);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(6);
            
            position = index;
                        
            highlight(7);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(7);
            
            DrawingPanel.comp1 = index;
            ModelFrame.drawing.repaint();
            
          while (position > 0 && Vehicle.vehicle.compareTo(list[position - 1], temp) == 1) 
          {
                highlight(9);
                try {
                    Thread.sleep(speed);
                } catch (Exception e) {
                }
                deHighlight(9);
                
                list[position] = list[position - 1];
                ModelFrame.drawing.repaint();
                highlight(10);
                try {
                    Thread.sleep(speed);
                } catch (Exception e) {
                }
                deHighlight(10);
                
                position--;
                DrawingPanel.comp2 = position;
                ModelFrame.drawing.repaint();
            }
            
            list[position] = temp;
            ModelFrame.drawing.repaint();
            
            highlight(12);
            try {
                Thread.sleep(speed);
            } catch (Exception e) {
            }
            deHighlight(12);
                        
            ModelFrame.drawing.repaint();
        }
        DrawingPanel.sn = list.length;
        ModelFrame.drawing.repaint();
        highlight(14);
    }
    
        // this is a method to highlight the current line that the thread is currently on
        // so that the user can follow what is happening.
    public static void highlight(int i) {
        // setting the current line as an element
        Element e = section.getElement(i);
        // getting start of the line.
        int rangeStart = e.getStartOffset();
        // getting end of the line
        int rangeEnd = e.getEndOffset();
        
         //these 3 lines together slect the line from start to end and highligh using
        // the colour gray.
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
        StyleConstants.setBackground(attrs, Color.LIGHT_GRAY); // change to white
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
    }
    
    // the deHighlight method is initially the same as the highlight method but it changes
    // the background to white to give the illusion of it being highlighted.
    public static void deHighlight(int i) {
        Element e = section.getElement(i);
        int rangeStart = e.getStartOffset();
        int rangeEnd = e.getEndOffset();

        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
        StyleConstants.setBackground(attrs, Color.WHITE); // change to white
        sdoc.setCharacterAttributes(rangeStart, (rangeEnd - rangeStart), attrs, false);
    }
    
    // method that is called in the jsldier actionlistner to get the current speed
    // of the threads by way to sleeping the thread.
    public static void getV(int v)
    {
        int i = v * 75;
        speed = 800 - i;
    }
}   //End of InsertionSort class.